package com.nb.controller;

import com.nb.dto.AssignAclDTO;
import com.nb.dto.CreateRoleDTO;
import com.nb.dto.UpdateRoleDTO;
import com.nb.entity.Role;
import com.nb.result.Result;
import com.nb.service.RoleService;
import com.nb.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public Result getRoleList() {
        List<Role> roleList = roleService.list();
        return Result.success(roleList);
    }

    @PostMapping("/create")
    public Result createRole(@Validated @RequestBody CreateRoleDTO dto) {
        Long roleId = roleService.createRole(dto);
        return Result.success(roleId);
    }

    @PutMapping("/update")
    public Result updateRole(@Validated @RequestBody UpdateRoleDTO dto) {
        boolean result = roleService.updateRole(dto);
        return Result.success(result);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteRole(@PathVariable Long id) {
        boolean result = roleService.deleteRole(id);
        return Result.success(result);
    }

    @GetMapping("/detail/{id}")
    public Result getRoleDetail(@PathVariable Long id) {
        RoleVO roleVO = roleService.getRoleDetail(id);
        return Result.success(roleVO);
    }

    @PostMapping("/assignAcl")
    public Result assignAcl(@Validated @RequestBody AssignAclDTO dto) {
        boolean result = roleService.assignAcl(dto);
        return Result.success(result);
    }
}
